Memory Management in Operating System

Concepts, Techniques, Types, and Importance

Introduction

Memory management is one of the most fundamental responsibilities of an Operating System. Every program requires memory to store instructions and data. Since memory is limited and shared among multiple processes, the operating system must manage it efficiently.

What is Memory Management?

Memory management is the process of allocating, tracking, and freeing main memory (RAM) for processes running in a system. It ensures efficient use of memory and prevents processes from interfering with each other.

Objectives of Memory Management

Memory Hierarchy in Computer System

The memory hierarchy represents the organization of different storage components based on speed, cost, and capacity. The diagram below shows how memory is arranged from fastest to slowest in a computer system.

Memory Hierarchy Diagram showing Registers, Cache, Main Memory, SSD, Magnetic Disk, Optical Disk and Magnetic Tapes
Figure: Memory Hierarchy in Computer System

Explanation of Memory Hierarchy

The memory hierarchy is designed to achieve a balance between high performance and low storage cost. As we move upward in the hierarchy, memory becomes faster and more expensive but smaller in size. As we move downward, memory becomes slower, cheaper, and larger in capacity.

Registers

Registers are the fastest storage units located inside the CPU. They store instructions and data that are currently being executed by the processor.

Cache Memory

Cache memory stores frequently accessed data and instructions to reduce the time required to access main memory, thereby improving system performance.

Main Memory (RAM)

Main memory holds active programs and data. It is volatile in nature and loses its contents when the system is powered off.

Electronic Disk (SSD)

Electronic disks provide fast non-volatile storage and are commonly used to store operating systems and applications.

Magnetic Disk

Magnetic disks offer large storage capacity at a lower cost and are widely used for permanent data storage.

Optical Disk

Optical disks are removable storage devices mainly used for data distribution and backups.

Magnetic Tapes

Magnetic tapes are used for archival purposes where large amounts of data need to be stored for a long duration at minimal cost.

Importance of Memory Hierarchy

Memory hierarchy allows the operating system to keep frequently used data in fast memory while storing less-used data in slower storage, resulting in better system efficiency and performance.

Logical Address vs Physical Address

When a program runs in a computer, the CPU does not directly access the real memory location. Instead, it uses a logical address which is later converted into a physical address.

logical and physical address
Figure: mapping of Logical Address to Physical Address

What is a Logical Address?

A logical address is generated by the CPU during program execution. It is a virtual address and does not represent the actual location in memory.

Example Logical Address: 236

What is a Physical Address?

A physical address is the real address in RAM where the data is stored and accessed.

Role of MMU (Memory Management Unit)

The MMU converts the logical address into a physical address using a base value stored in the relocation register.

Base Address (Relocation Register): 14000
Logical Address: 236
Physical Address = Base + Logical = 14236

Memory Management Techniques

Memory Management Technique,contiguous, non-contiguous,fixed partition,variable partition,paging, segmentation

Contiguous Memory Allocation

Each process is allocated a single continuous block of memory. Although simple, this method suffers from fragmentation. In a computer system, main memory (RAM) is divided into small blocks. When processes are loaded into memory, each process is given a continuous (one after another) space. This method is called contiguous memory allocation.

logical and physical address

In the diagram, memory is divided into parts labeled F1 to F9. Different processes are placed inside these blocks:

Non-Contiguous Memory Allocation

Processes are divided into parts and stored in different memory locations, improving memory utilization.In a computer system, memory is not always assigned in one continuous block. Sometimes, a process is divided into smaller parts and stored in different locations in RAM. This method is called non-contiguous memory allocation.

logical and physical address

In the diagram:

These parts are stored in different free areas of memory.

Paging

Paging divides memory into fixed-size pages and frames. A page table maps pages to frames, eliminating external fragmentation.
aging is a memory management technique used to store and access data efficiently. It allows a process to be divided into smaller parts so that memory can be used in a flexible way. In paging, memory is divided into two parts:

Each page has the same size as a frame.
Paging in Operating System
In the diagram:

Working of Paging in Operating System

Working of Paging in Operating Systemm

Paging is a memory management method that converts a logical address generated by the CPU into a physical address in main memory. This process helps the operating system use memory efficiently.

1. Logical Address Generation

The CPU generates a logical address which is divided into two parts:

Logical Address = Page Number + Offset

In this example, the logical address is 13 bits:

2. Page Table Mapping

The page number is sent to the page table. The page table stores the corresponding frame number where the page is located in RAM.

Example: Page 2 → Frame 2

3. Physical Address Formation

After finding the frame number, the system combines:

Physical Address = Frame Number + Offset

In this example:

4. Accessing Main Memory

Using the physical address, the system accesses the correct frame in main memory and fetches the required data.

Segmentation

Segmentation in Operating System (OS) is a memory management technique where a program is divided into smaller logical parts called segments. Segmentation divides a program into logical units such as code, data, and stack segments. Each segment has its own base and limit.
Segmentation means breaking a program into meaningful sections like:

Each part is stored separately in memory.

Working of Paging in Operating System

Working of Paging in Operating System

Segmentation is a memory management technique where a logical address generated by the CPU is converted into a physical address using a segment table.

Steps Involved

  1. Logical Address from CPU

    • Segment Number (s) = 1
    • Offset (d) = 400
    • Logical Address = (1, 400)
  2. Access Segment Table

    • Base Address = 1800
    • Segment Size = 400
  3. Check Validity

    • Check if Offset ≤ Segment Size
    • 400 ≤ 400 → Valid
    • If offset is greater → Segmentation Fault
  4. Calculate Physical Address

    • Physical Address = Base Address + Offset
    • Physical Address = 1800 + 400 = 2200
  5. Access Physical Memory

    • System accesses memory at address 2200
    • Data is retrieved successfully

Invalid Case

Virtual Memory

Virtual memory allows execution of programs larger than physical memory by loading only required portions into RAM when needed.

Swapping

Swapping temporarily moves processes between main memory and secondary storage to free up space for higher-priority processes.

Fragmentation

Memory Protection

Memory protection ensures that processes access only their allocated memory using hardware and software mechanisms.

Advantages of Memory Management

Conclusion

Memory management plays a vital role in the performance and reliability of an operating system. By using techniques such as paging, segmentation, and virtual memory, the OS ensures optimal utilization of limited memory resources.

Visit Our YouTube Channel